var express = require('express');
var router = express.Router();
const orderService = require("../service/orderService");

/**
 * @swagger
 * /order:
 *    get:
 *      tags:
 *      - order
 *      summary: 条件查找
 *      parameters:
 *      - name: id
 *        in: query
 *        description: 订单id
 *        required: true
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.get('/', async function (req, res, next) {
    let { type, value, current, pageSize } = req.query;
    let params = {};

    if (type == "status") {
        params[type] = value;
    } else if (type == "startTime") {
        if (value[0]) {
            params.startTime = { $gte: value[0] };
        }
        if (value[1]) {
            params.startTime = { ...params.startTime, $lte: value[1] };
        }
    } else if (type == "finishTime") {
        if (value[0]) {
            params.finishTime = { $gte: value[0] };
        }
        if (value[1]) {
            params.finishTime = { ...params.finishTime, $lte: value[1] };
        }
    }

    let data = await orderService.find(params, {
        current: current || 1,
        pageSize: pageSize || 5
    })

    res.send({
        data
    })
});

/**
 * @swagger
 * /order/all:
 *    get:
 *      tags:
 *      - order
 *      summary: 查找全部
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.get("/all", async function (req, res, next) {
    let { year } = req.query;
    let params = {};
    if (year) {
        params["finishTime"] = { $regex: year };
    }
    let data = await orderService.findAll(params);
    res.send({
        data
    })
})

/**
 * @swagger
 * /order:
 *    post:
 *      tags:
 *      - order
 *      summary: 订单管理
 *      requestBody:
 *        required: true
 *        content:
 *          application/x-www-form-urlencoded:
 *            schema:
 *              type: object
 *              properties:
 *                user: 
 *                  type: string
 *                totalPrice: 
 *                  type: number
 *                count: 
 *                  type: number
 *                status: 
 *                  type: number
 *                startTime: 
 *                  type: string
 *                finishTime: 
 *                  type: string
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.post("/", async function (req, res, next) {
    let params = req.body;
    // console.log(params);
    if (!params.remarks) {
        params.remarks = "无"
    }
    let result = await orderService.insert(params);
    res.send(result);
})

router.get("/:id", async function (req, res, next) {
    let id = req.params.id;
    let data = await orderService.findById(id);
    res.send({
        data
    })
})

router.put("/:id", async function (req, res, next) {
    let id = req.params.id;
    let params = req.body;

    let result = await orderService.update({ id, ...params });
    res.send({
        result
    })
})

/**
 * @swagger
 * /order/{id}:
 *    delete:
 *      tags:
 *      - order
 *      summary: 删除
 *      parameters:
 *      - name: id
 *        in: path
 *        description: 订单id
 *        required: true
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.delete("/:id", async function (req, res, next) {
    let id = req.params.id;

    let result = await orderService.remove(id);
    res.send({
        result
    })
})

module.exports = router;
